﻿eventModule.controller("showvacancyController", function ($scope, $location, EventService) {
    EventService.getVacancys().then(function (vacancys) { $scope.vacancys = vacancys }, function ()
    { alert('Error Occured.') });

    $scope.deleteVacancy = function (vacancy) {
        if (confirm('Are you sure you want to delete this vacancy?')) {
            var vacancy_id = vacancy.vacancy_id;
            EventService.deleteVacant(vacancy_id).then(function () {
                var index = $scope.vacancys.indexOf(vacancy);
                $scope.vacancys.splice(index, 1);
            }, function () { alert('Deletion is only allowed when Planned.') });
        } else {
            // Do nothing!
        }
    };

    $scope.copyVacancy = function (vacancy) {
        var vacancy_id = vacancy.vacancy_id;
        if (confirm('Are you sure you want to copy this vacancy (' + vacancy_id + ') ?')) {

            EventService.copyVacant(vacancy_id).then(function (data) {
                var copied = data.vacancy_id;
                alert('New vacancy (' + copied + ') created.');
                $scope.vacancys.push(data);
            }, function () { alert('Error occured') });
        } else {
            // Do nothing!
        }
    };

});